package com.chinatgl.latte.net.rx;

import android.content.Context;

import com.chinatgl.latte.net.RestClient;
import com.chinatgl.latte.net.RestCreator;
import com.chinatgl.latte.net.callback.IError;
import com.chinatgl.latte.net.callback.IFailure;
import com.chinatgl.latte.net.callback.IRequest;
import com.chinatgl.latte.net.callback.ISuccess;
import com.chinatgl.latte.ui.LoaderStyle;

import java.io.File;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 文件描述: 构建网络请求
 * 作者: Created by 杨伟乔 on 2017/8/7 0007.
 * 版本号: 1.0
 */

public class RxRestClientBuilder {

    private String mUrl = null;
    private WeakHashMap<String, Object> PARAMS = RestCreator.getParams();

    private RequestBody mBody = null;
    private Context mContext = null;
    private LoaderStyle mLoaderStyle = null;
    private File mFile = null;


    RxRestClientBuilder() {

    }

    /**
     * url地址
     *
     * @param url
     * @return
     */
    // TODO: 2017/8/8 0008 url
    public final RxRestClientBuilder url(String url) {
        this.mUrl = url;
        return this;
    }

    /**
     * @param params 传入参数map
     * @return
     */
    public final RxRestClientBuilder params(WeakHashMap<String, Object> params) {
        PARAMS.putAll(params);
        return this;
    }

    /**
     * 传入原始的参数
     *
     * @param key
     * @param value
     * @return
     */
    public final RxRestClientBuilder params(String key, Object value) {

        PARAMS.put(key, value);
        return this;
    }

    public final RxRestClientBuilder raw(String raw) {
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
        return this;
    }
    //文件直接上传
    public final RxRestClientBuilder file(File file) {
        this.mFile = file;
        return this;
    }
    //根据地址上传文件
    public final RxRestClientBuilder file(String file) {
        this.mFile = new File(file);
        return this;
    }


    public final RxRestClientBuilder loader(Context context, LoaderStyle loaderStyle) {
        this.mContext = context;
        this.mLoaderStyle = loaderStyle;
        return this;
    }

    public final RxRestClientBuilder loader(Context context) {
        this.mContext = context;
        this.mLoaderStyle = LoaderStyle.BallClipRotatePulseIndicator;
        return this;
    }

    public final RxRestClient build() {
        return new RxRestClient(mUrl, PARAMS , mBody,mFile, mLoaderStyle, mContext);
    }
}
